package org.meetunexpectedly.entity.vo;

import lombok.Data;

import java.text.MessageFormat;

/**
 * @Description: 响应类，处理响应数据
 * @Author: pupil
 * @Date: 2024/08/03 下午 9:11
 */
@Data
public class Result<T> {
    private static final long serialVersionUID = 22L;

    // 类型
    private T type;

    // 响应码
    private int code;

    // 返回状态
    private Boolean state;

    // 返回消息
    private String message;

    // 返回数据
    private Object data;

    private long count;

    public Result() {
    }

    /**
     * 成功
     * @return
     */
    public static Result ok(){
        Result result = new Result();
        result.setCode(0);
        result.setState(true);
        result.setMessage("成功");
        return result;
    }

    /**
     * 失败
     * @return
     */
    public static Result error(){
        Result result = new Result();
        result.setCode(201);
        result.setState(false);
        result.setMessage("失败");
        return result;
    }

    public Result code(int code){
        this.setCode(code);
        return this;
    }

    public Result state(Boolean state){
        this.setState(state);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result message(String message, Object... objects){
        this.setMessage(MessageFormat.format(message, objects));
        return this;
    }

    public Result data(Object result){
        this.setData(result);
        return this;
    }

    public Result count(long count){
        this.setCount(count);
        return this;
    }

}
